package com.regtwo.admin.sqlapp;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase sqLiteDatabase;
    private SqlAppHelper sqlAppHelper;
    private File sdcardPath ;
    private int i = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sdcardPath = Environment.getExternalStorageDirectory();
        sqlAppHelper = new SqlAppHelper(this,sdcardPath+"/gtq-1.db",null,1);
        sqLiteDatabase = sqlAppHelper.getWritableDatabase();
    }

    public void writein(View view) {
        String sql = "insert into users (uname) values(?)";
        try{
            sqLiteDatabase.beginTransaction();
            sqLiteDatabase.execSQL(sql,new Object[]{"GTQ-"+i});
            sqLiteDatabase.setTransactionSuccessful();

        }catch (Exception e){

        }finally {
            sqLiteDatabase.endTransaction();
        }

    }
    public void readDB(View view) {
        String sql="select uname,id from users where id>?";
        Cursor c=sqLiteDatabase.rawQuery(sql, new String[]{"2"});
        while (c.moveToNext()){
            String uname=c.getString(0);
            int id=c.getInt(c.getColumnIndex("id"));
            Log.i("gtq-1", "uanme:" + uname + "........" + id);
        }
    }


    public void query(View view) {
        Intent intent = new Intent("qa");
        startActivity(intent);
        /*Uri uri = Uri.parse("content://gtq.cn/users");
        Cursor c = getContentResolver().query(uri,null,null,null,null);
        while (c.moveToNext()){
            String uname=c.getString(0);
            int id=c.getInt(c.getColumnIndex("id"));
            Log.i("gtq-2", "uanme:" + uname + "........" + id);
        }*/

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sqLiteDatabase.close();
    }
}
